/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.mapper.ClazzandstudentMapper;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.StudentnoteService;
import me.zhengjie.modules.system.domain.vo.StudentnoteQueryCriteria;
import me.zhengjie.modules.system.mapper.StudentnoteMapper;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author shitou
* @date 2024-08-15
**/
@Service
@RequiredArgsConstructor
public class StudentnoteServiceImpl extends ServiceImpl<StudentnoteMapper, Studentnote> implements StudentnoteService {

    private final StudentnoteMapper studentnoteMapper;

    private final ClazzandstudentMapper clazzAndStudentMapper;

    private final UserMapper userMapper;

    @Override
    public PageResult<Studentnote> queryAll(StudentnoteQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(studentnoteMapper.findAll(criteria, page));
    }

    @Override
    public List<Studentnote> queryAll(StudentnoteQueryCriteria criteria){

        if(criteria.getIsAll() != 1){
            criteria.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        } else {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("userId",SecurityUtils.getCurrentUserId());
            Clazzandstudent clazzandstudent = clazzAndStudentMapper.selectOne(queryWrapper);

            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("clazzId",clazzandstudent.getClazzId());
            List<Clazzandstudent> clazzandstudentList = clazzAndStudentMapper.selectList(queryWrapper1);
            List<Integer> userIds = new ArrayList<>();
            for(Clazzandstudent clazzStudent:clazzandstudentList){
                userIds.add(Math.toIntExact(clazzStudent.getUserId()));
            }
            criteria.setUserIds(userIds);
            criteria.setIsOpen(1);
        }

        List<Studentnote> studentnoteList = studentnoteMapper.findAll(criteria);
        List<Integer> allUserIds = new ArrayList<>();
        QueryWrapper queryWrapper1 = new QueryWrapper<>();



        for(Studentnote studentnote:studentnoteList){
            allUserIds.add(studentnote.getCreateUserId());

        }
        queryWrapper1.in("user_id",allUserIds);
        List<User> userList = userMapper.selectList(queryWrapper1);
        for(Studentnote studentnote:studentnoteList){
            for(User user:userList){
                if(studentnote.getCreateUserId() == Math.toIntExact(user.getId())){
                    studentnote.setCreateName(user.getUsername());
                    studentnote.setIcon(user.getAvatarPath());
                }
            }

        }
        return studentnoteList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Studentnote resources) {
        resources.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Studentnote resources) {
        Studentnote studentnote = getById(resources.getId());
        studentnote.copy(resources);
        resources.setCreateUserId(Math.toIntExact(SecurityUtils.getCurrentUserId()));
        saveOrUpdate(studentnote);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Studentnote> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Studentnote studentnote : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" teachid",  studentnote.getTeachId());
            map.put(" teachchapterid",  studentnote.getTeachChapterId());
            map.put(" teachsubsectionid",  studentnote.getTeachSubsectionId());
            map.put(" createuserid",  studentnote.getCreateUserId());
            map.put(" createtime",  studentnote.getCreateTime());
            map.put(" isopen",  studentnote.getIsOpen());
            map.put(" content",  studentnote.getContent());
            map.put(" coordinate",  studentnote.getCoordinate());
            map.put(" notecontent",  studentnote.getNoteContent());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}